import Vue from "vue";
import VueRouter from "vue-router";
// import Login from "../views/Login";
// import Layout from "../views/Layout";
// import Home from "../views/Home";
// import User from "../views/User";
// import Search from "../views/Search";
// import SearchResult from "../views/Search/SearchResult";
// import ArticleDetail from "@/views/ArticleDetail";
// import UserEdit from "@/views/User/UserEdit";
// import Chat from "@/views/Chat";
import { getToken } from "@/utils/token.js";

Vue.use(VueRouter);

const routes = [
  //一级路由
  {
    path: "/",
    redirect: "/layout/home",
  },
  {
    path: "/login",
    component: () => import(/* webpackChunkName: "login" */ "@/views/Login"),
    //路由独享守卫
    beforeEnter: (to, from, next) => {
      if (getToken()?.length > 0) {
        // next(false); //留在原地
        next("/layout/home"); //跳转到首页
      } else {
        next();
      }
    },
  },
  {
    path: "/layout",
    component: () => import(/* webpackChunkName: "layout" */ "@/views/Layout"),
    //二级路由
    children: [
      {
        path: "home",
        component: () => import(/* webpackChunkName: "home" */ "@/views/Home"),
        meta: {
          scrollT: 0, //保存首页离开时，滚动条的位置
        },
      },
      {
        path: "user",
        component: () => import(/* webpackChunkName: "User" */ "@/views/User"),
      },
    ],
  },
  {
    path: "/search",
    component: () => import(/* webpackChunkName: "Search" */ "@/views/Search"),
  },
  //搜索结果页
  {
    path: "/search_result/:kw",
    component: () =>
      import(
        /* webpackChunkName: "SearchResult" */ "@/views//Search/SearchResult"
      ),
  },
  //文章详情页
  {
    path: "/detail",
    component: () =>
      import(/* webpackChunkName: "ArticleDetail" */ "@/views/ArticleDetail"),
  },
  //用户-编辑资料
  {
    path: "/user_edit",
    component: () =>
      import(/* webpackChunkName: "UserEdit" */ "@/views/User/UserEdit"),
  },

  //聊天机器人
  {
    path: "/chat",
    component: () => import(/* webpackChunkName: "Chat" */ "@/views/Chat"),
  },
];

const router = new VueRouter({
  routes,
});

//路由前置守卫
// router.beforeEach((to, from, next) => {
//   //当前是登录状态则不让跳转到登录页
//   if (getToken()?.length > 0 && to.path === "/login") {
//     next(false); //留在原地
//next("/layout/home"); //跳转到首页
//   } else {
//     next();
//   }
// });

export default router;
